package com.rchat.pocmini.activity.ble;

import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.rchat.pocmini.R;
import com.rchat.pocmini.bean2eventbus.BleBean;
import com.rchat.pocmini.bean2eventbus.ConnectBleBean;
import com.rchat.pocmini.bean2eventbus.ScanBleStateBean;
import com.rchat.pocmini.common.mvp.BaseMvpActivity;
import com.rchat.pocmini.service.PocService;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by ljd on 2017/8/29.
 * Modify by sudf on 2017/12/19
 *
 * @describe:
 */

public class BleActivity extends BaseMvpActivity<BlePresenterImpl, BleModelImpl>
        implements BleContract.View ,
        ScanBleResultAdapter.IItemTouchHelperAdapter{

    @BindView(R.id.layout_back)
    LinearLayout llBack;
    @BindView(R.id.btn_scan)
    Button mBtnScan;
    @BindView(R.id.lv_ble_scan_result)
    ListView mLvBleScanResult;

    private ScanBleResultAdapter mAdapter;
    private List<BleBean> mScanResultList;


    @Override
    public void initPresenter() {
        mPresenter.setVM(this, mModel);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_ble;
    }

    @Override
    public void initView() {
        mScanResultList = new ArrayList<>();
        mAdapter = new ScanBleResultAdapter(this, mScanResultList);
        mAdapter.setItemTouchHelperListener(this);
        mLvBleScanResult.setAdapter(mAdapter);
    }

    @Override
    public void doAfterInitView() {

    }

    @OnClick({R.id.layout_back,
            R.id.btn_scan})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.layout_back:
                finish();
                break;
            case R.id.btn_scan:
                mScanResultList.clear();
                onUpdateBleList();
                mPresenter.scan();
                break;
        }
    }


    @Override
    public void bleDataAdd(List<BleBean> list) {
        mScanResultList.clear();
        mScanResultList.addAll(list);
        onUpdateBleList();
    }

    @Override
    public void scanStatus(int b) {
        switch (b) {
            case ScanBleStateBean.STOP_SCAN: //停止扫描
                mBtnScan.setText(R.string.scan_complete);
                mBtnScan.setEnabled(true);
                break;
            case ScanBleStateBean.SCANNING://正在扫描
                mBtnScan.setText(R.string.scanning);
                mBtnScan.setEnabled(false);
                break;
        }
    }

    @Override
    public void onUpdateBleList() {
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onBleConnected(List<BleBean> list) {
        mScanResultList.clear();
        mScanResultList.addAll(list);
        onUpdateBleList();
    }

    @Override
    public void onClickBleConnect(BleBean bean) {
        if (bean == null){
            return;
        }
        if (bean.isConnected()){
            mPresenter.onDisConnectedBle(bean);
        } else {
            ConnectBleBean b = new ConnectBleBean();
            b.setDeviceAddress(bean.getResult().getAddress());
            b.setTarget(this.getClass(), PocService.class, "connectBle");
            b.setCurrentEvent(ConnectBleBean.CONNECT_BLE);
            EventBus.getDefault().post(b);
        }

    }
}
